package com.biz.primus.management.tms.controller;

import com.biz.primus.common.utils.DateUtil;
import com.biz.primus.common.vo.SpringPageVO;
import com.biz.primus.management.base.controller.AbstractBaseController;
import com.biz.primus.management.tms.feign.EmployeeFeignClient;
import com.biz.primus.model.depot.vo.*;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * 用户信息查询
 * Created by root on 17-2-8.
 */
@Controller
@RequestMapping("users")
//@Secured("ROLE_USER")
@Slf4j
public class UserController extends AbstractBaseController {

    private final EmployeeFeignClient employeeFeignClient;

    @Autowired
    public UserController(EmployeeFeignClient employeeFeignClient) {
        this.employeeFeignClient = employeeFeignClient;
    }

    @GetMapping({"", "list"})
//    @PreAuthorize("hasAuthority('OPT_USER_LIST')")
    public ModelAndView findAll(EmployeePageReqVo reqVo) {
        SpringPageVO<EmployeeTmsListRespVo> page = employeeFeignClient.tmsPageResult(reqVo);
        return new ModelAndView("user/list").addObject("users", page);
    }

    @ResponseBody
    @PostMapping("generateQrCode")
    //    @PreAuthorize("hasAuthority('OPT_USER_QR')")
    public QrCodeVo generateQrCode(@ApiParam(value = "员工编码") @RequestParam("employeeCode") String employeeCode) {
        return employeeFeignClient.generateQrCode(employeeCode);
    }

    @GetMapping("spreadData")
    public ModelAndView getSpreadData(EmpSpreadPageReqVo reqVo) {
        ModelAndView mav = new ModelAndView("user/spread");
        SpringPageVO<EmpSpreadRespVo> spreadData = employeeFeignClient.getSpreadData(reqVo);
        return mav.addObject("springPage", spreadData);
    }

    @PostMapping("downloadSpreadData")
    public void downloadSpreadData(EmpSpreadPageReqVo reqVo, HttpServletRequest request, HttpServletResponse response) {
        SpringPageVO<EmpSpreadRespVo> spreadData;
        List<EmpSpreadRespVo> lists = Lists.newArrayList();
        do {
            spreadData = employeeFeignClient.getSpreadData(reqVo);
            lists.addAll(spreadData.getContent());
        } while (spreadData.hasNext());
        SXSSFWorkbook wb = new SXSSFWorkbook();
        SXSSFSheet sheet = wb.createSheet();
        SXSSFRow header = sheet.createRow(0);
        header.createCell(0).setCellValue("注册会员ID");
        header.createCell(1).setCellValue("手机号码");
        header.createCell(2).setCellValue("门店员工编号");
        header.createCell(3).setCellValue("姓名");
        header.createCell(4).setCellValue("所属门店");
        header.createCell(5).setCellValue("扫码注册时间");
        for (int i = 1; i <= lists.size(); i++) {
            SXSSFRow row = sheet.createRow(i);
            EmpSpreadRespVo spreadDataItem = lists.get(i - 1);
            row.createCell(0).setCellValue(String.valueOf(spreadDataItem.getRegisterId()));
            row.createCell(1).setCellValue(spreadDataItem.getMobile());
            row.createCell(2).setCellValue(spreadDataItem.getEmpCode());
            row.createCell(3).setCellValue(spreadDataItem.getEmpName());
            row.createCell(4).setCellValue(spreadDataItem.getDepotName());
            row.createCell(5).setCellValue(spreadDataItem.getRegisterTime());
        }
        try (ServletOutputStream outputStream = response.getOutputStream()) {
            String fileName = String.format("emp_%s", DateUtil.getNowFmtDatetime("yyyy-MM-dd HH:mm:ss"));
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
            wb.write(outputStream);
            log.error("生成Excel文件过程成功");
        } catch (Exception e) {
            log.error("生成Excel文件过程失败", e);
        }
    }

}
